package com.slz3.business.module.wmsManagement.wmsWarningConfig.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.wmsManagement.wmsWarningConfig.domain.WmsWarningConfig;
import com.slz3.business.module.wmsManagement.wmsWarningConfig.domain.WmsWarningConfigFactory;
import com.slz3.business.module.wmsManagement.wmsWarningConfig.steward.WmsWarningConfigSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Slf4j
@Service
public class WmsWarningConfigServiceImpl implements WmsWarningConfigService {
    @Override
    public WmsWarningConfig save(WmsWarningConfig wmsWarningConfig) {
        return Repositorys.wmsWarningConfig.save(WmsWarningConfigFactory.initBaseFields(wmsWarningConfig));
    }

    @Override
    public Page<WmsWarningConfig> page(MultiValueMap<String, String> params) {
        Page<WmsWarningConfig> wmsWarningConfigPage = Repositorys.wmsWarningConfig.findAll(
                WmsWarningConfigSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return wmsWarningConfigPage;
    }


    @Override
    public WmsWarningConfig modify(WmsWarningConfig wmsWarningConfigNew) {
        return Repositorys.wmsWarningConfig.saveAndFlush(WmsWarningConfigFactory.initBaseFields(wmsWarningConfigNew));
    }

    @Override
    public Optional<WmsWarningConfig> single(UUID uuid) {
        return Repositorys.wmsWarningConfig.findById(uuid);
    }

    @Override
    public List<WmsWarningConfig> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.wmsWarningConfig.findAllById(uuids);
    }

    @Override
    public List<WmsWarningConfig> delete(List<WmsWarningConfig> wmsWarningConfigList) {
        wmsWarningConfigList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            WmsWarningConfigFactory.initBaseFields(item);
        });
        return Services.wmsWarningConfigTransactional.batchModify(wmsWarningConfigList);
    }

}
